#include <linux/module.h>
#include <linux/uaccess.h>
#include <linux/platform_device.h>


static int major = 0;	//主版本
static int ker_val = 0;//数值
static struct class* class_for_my_print; //class


//读取
static ssize_t my_print_read(struct file* file, char __user* buf, size_t size, loff_t* offset) {
	copy_to_user(buf, &ker_val, size);
	return size;
}

//写入
static ssize_t my_print_write(struct file* file, const char __user* buf, size_t size, loff_t* offset) {
	copy_from_user(&ker_val, buf, size);
	return size;
}

static struct file_operations my_print_fops = {
	.owner = THIS_MODULE,
	.read = my_print_read,
	.write = my_print_write
};

//平台总线

static int my_print_probe(struct platform_device* pdev) {

	printk("my_print_probe ! \n");
	major = register_chrdev(0, "my_print_drv", &my_print_fops);					//	注册字符设备
	class_for_my_print = class_create(THIS_MODULE, "my_print.class"); 			// 	"/sys/class/my_print_class"
	device_create(class_for_my_print, NULL, MKDEV(major, 0), NULL, "my_print");	//	"/dev/my_print"
	return 0;
}

static int my_print_remove(struct platform_device* pdev) {

	printk("my_print_remove ! \n");
	device_destroy(class_for_my_print, MKDEV(major, 0));
	class_destroy(class_for_my_print);
	unregister_chrdev(major, "my_print_drv");
	return 0;
}

// 平台驱动程序结构体
static struct platform_driver my_print_driver = {
	.probe = my_print_probe,
	.remove = my_print_remove,
	.driver = {
		.name = "my_print_platform_driver"
	}
};

static int __init my_print_init(void) {
	int err;
	printk("my_print_init ! \n");
	err = platform_driver_register(&my_print_driver);
	return err;
}

static void __exit my_print_exit(void) {
	printk("my_print_exit ! \n");
	platform_driver_unregister(&my_print_driver);
}


module_init(my_print_init);
module_exit(my_print_exit);


MODULE_LICENSE("GPL");


